package com.sian.datasource;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.springframework.jms.core.JmsTemplate;

/**
 *@see 把前台所有操作的原生SQL记录到指定的文件中
 *
 */
public class C1DataSource implements DataSource{
    
    private DataSource  datasource;
    private Connection  injectConnect;
    
    private JmsTemplate jmsTemplate;
	
	
	public Connection getConnection() throws SQLException {
		// TODO Auto-generated method stub
		Connection connection = null;
		
		if(datasource!=null){
		    connection = this.datasource.getConnection();
		}else if(injectConnect!=null) {
			connection = injectConnect;
		}else{
			throw new SQLException("connection or datasource is required inject!");
		}
		
		C1CoreFactory  coreFactory = new C1CoreFactory();
		C1WriteLog.setConnection(connection);//为写日志时做准备
		C1WriteLog.setJmsTemplate(jmsTemplate);
		
		return coreFactory.getConnection(connection);
	}

	
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return this.datasource.getConnection(username, password);
	}

	
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return this.datasource.getLogWriter();
	}

	
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return this.datasource.getLoginTimeout();
	}

	
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
		this.datasource.setLogWriter(out);
	}

	
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
	   this.datasource.setLoginTimeout(seconds);
	}

	
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return this.datasource.isWrapperFor(iface);
	}

	
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return this.datasource.unwrap(iface);
	}

	public DataSource getDatasource() {
		return datasource;
	}

	public void setDatasource(DataSource datasource) {
		this.datasource = datasource;
	}

	public Connection getInjectConnect() {
		return injectConnect;
	}

	public void setInjectConnect(Connection injectConnect) {
		this.injectConnect = injectConnect;
	}


	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}


	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}


	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}